package com.ywjt.platformbusiness.widget;
import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ywjt.platformbusiness.R;
import com.ywjt.platformbusiness.base.BaseBottomDialog;
import com.ywjt.platformbusiness.store.adapter.InComeMonthSelectAdapter;
import com.ywjt.platformbusiness.store.model.MonthSelectModel;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

public class IncomMonthSelectBottomDialog extends BaseBottomDialog {
private RecyclerView rcCommon;
private InComeMonthSelectAdapter adapter;
private Context context;
private List<MonthSelectModel> data;
private TextView tvYear;
private ImageView ivNext,ivLast;
private TextView tvConfirm;
private int currentPosition=0;

private int year=2021;
    private ClickListenerInterface clickListenerInterface;

    public interface ClickListenerInterface {
        void  confirm(String year);
    }

    public IncomMonthSelectBottomDialog(Context context) {
        this.context=context;
    }

    @Override
    public int getLayoutRes() {
        return R.layout.dialog_incom_month_select;
    }
    MonthSelectModel monthSelectModel;
    @Override
    public void bindView(View v) {
        tvYear=v.findViewById(R.id.tvYear);
        ivNext=v.findViewById(R.id.ivNext);
        tvConfirm=v.findViewById(R.id.tvConfirm);
        ivLast=v.findViewById(R.id.ivLast);
        rcCommon=v.findViewById(R.id.rcCommon);
        data=new ArrayList<>();
        for (int i=1;i<=12;i++){
            if (i==1){
                monthSelectModel=new MonthSelectModel(i+"月",true);
            }else {
                 monthSelectModel=new MonthSelectModel(i+"月",false);
            }
            data.add(monthSelectModel);
        }

        adapter=new InComeMonthSelectAdapter(data);
        rcCommon.setLayoutManager(new GridLayoutManager(context,4,RecyclerView.VERTICAL,false));
        rcCommon.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                currentPosition=position;
                for (int i=0;i<data.size();i++){
                    data.get(i).setSelect(false);
                }
                data.get(position).setSelect(true);
                adapter.notifyDataSetChanged();
            }
        });

        tvYear.setText(year+"年");
        ivNext.setOnClickListener(new clickListener());
        ivLast.setOnClickListener(new clickListener());
        tvConfirm.setOnClickListener(new clickListener());
    }

    public void setClicklistener(ClickListenerInterface clickListener) {
        clickListenerInterface = clickListener;
    }

    private class clickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.ivLast:
                    year--;
                    tvYear.setText(year+"年");
                    break;
                case R.id.ivNext:
                    year++;
                    tvYear.setText(year+"年");
                    break;
                case R.id.tvConfirm:
                    clickListenerInterface.confirm(year+"-"+computeData(currentPosition+1));
                    break;
                default:
                    break;
            }
        }
    }

    public String computeData(Integer data){
        String p="";
        switch (data){
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
            case 6:
            case 7:
            case 8:
            case 9:
                p="0"+data;
                break;
            case 10:
            case 11:
            case 12:
                p=data+"";
                break;
        }
        return p;
    }
}